<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace Lib;

/**
 * Description of Msg
 *
 * @author Boot
 */
class Msg {
    
    private $db;
    private $table;
    
    public function __construct( $db, $table ) {
        $this->db = $db;
        $this->table = $table;
    }
    
    public function GetSingleMsg ( $id ) {
        return $this->db->get( $this->table, [ 'nick', 'time', 'id', 'txt', 'hide' ] , [ 'id' => $id ]);
    }
    
    public function GetMsg ( $page ) {
        $start = ( $page -1 ) * 5;
        return $this->db->select( $this->table, [ 'nick', 'time', 'id', 'txt', 'hide' ], [ 'hide[!]' => 't', 'ORDER' => 'time DESC', 'LIMIT' => [ $start, 5 ] ]);
    }

    public function AddMsg( $nick, $time, $txt, $ip, $to_mail, $is_send, $hide ) {
        return $this->db->insert( $this->table, [ 'nick' => $nick, 'time' => $time, 'txt' => $txt, 'ip' => $ip, 'to_mail' => $to_mail, 'is_send' => $is_send, 'hide' => 'f']);
    }
    
    public function DelMsg ( $id ) {
        return $this->db->delete( $this->table, [ 'id' => $id ]);
    }
    
    public function HideMsg ( $id ) {
        return $this->db->update( $this->table, [ 'hide' => 't'], [ 'id' => $id ]);
    }
    
    public function GetMsgCount() {
        return $this->db->count( $this->table );
    }
}
